<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class Login extends CI_Controller {

	private $admin_table = 'admin_table';// = $this->config->item('admin_table');
	private $admin_user_field  = "username";
	private $admin_password_field = "password"; // 
	private $admin_password_cript = false; //
	
	public function __construct()
	{
		parent::__construct();
		
		$this->admin_table 			= $this->config->item('admin_table');
		$this->admin_user_field  	= $this->config->item('admin_user_field');
		$this->admin_password_field = $this->config->item('admin_password_field');
		$this->admin_password_cript = $this->config->item('admin_password_cript');
	}
	
	/**
	 * Index Page for this controller.
	 *
	 */
	public function index()
	{             
		$this->session->sess_destroy();
		if($this->input->post('login_submit')) //ako je upisao podatke za login
		{
			
			//Proveri da li su dobri
			$uname = $this->input->post('username');
			$psw   = $this->input->post('password');
			// ako je upisao username i password
			if($uname!="" && $psw !="")
			{
				//provera u bazi
				if($this->config->item('admin_database')) //da li su login podaci u bazi
				{
					// Podrazumevana tabela je definisana u config.php
                    // user_group = 1 => admin user, 2 => moderator
					$sql = "SELECT * FROM ".$this->admin_table . " 
							WHERE  user_group = 1  AND ". $this->admin_user_field . " = '$uname'  ";
					if($this->admin_password_cript)
						$sql .=" AND ".$this->admin_password_field." = MD5('$psw'); ";
					else
						$sql .=" AND ".$this->admin_password_field." = '$psw'; ";
					$query = $this->db->query($sql);
					
					$row = $query->row_array(); // dobijamo jedan rezultat
					if($row)
					{
						//Upisi da je logovan
						$this->session->set_userdata('admin_logged',true);
						$this->session->set_userdata('admin_username',$row[$this->admin_user_field]);
                        $this->session->set_userdata('admin_id',$row['id']);
                        // Upisi da je logovan i na front-u
                        $this->session->set_userdata('user_logged',true);
                        $this->session->set_userdata('user_id',$row['id']);
                        $this->session->set_userdata('username',$row['username']);
                        $this->session->set_userdata('name',$row['name']);
                        $this->session->set_userdata('user_group',$row['user_group']);
                        
                        
						// Upisi IP adresu sa koje je logovan kao i datum logovanja
						$sql = "UPDATE ".$this->admin_table . "
								SET ip_address = '".$this->session->userdata('ip_address')."',
									timestamp = NOW()
                                WHERE `id`= {$row['id']} ; ";
						$this->db->query($sql);
					}					
				}
				else //nema konekcije ka bazi proveri username i password iz config file-a
				{
					if(	$this->config->item('admin_username') == $uname && 
						$this->config->item('admin_password') == $psw )
					{
						//Upisi da je logovan
						$this->session->set_userdata('admin_logged',true);
						$this->session->set_userdata('admin_username',$this->config->item('admin_username'));
					}
				}
				
			}
					
			//Ako je korisnik uneo dobre podatke preusmeri ga na home stranicu		  
			redirect(base_url().'administration/home', 'refresh');
			
		}
		else //nije uneo dobre podatke opet idi na login
		{
			// Ovde moze da se doda brojac broja pokusaja
			$data['contents']  = "login";
			$this->session->sess_destroy();
		}
        
		$this->load->view('main',$data);
	}
    
        
    
    public function logout()
    {
        $this->session->sess_destroy();    
        redirect(base_url().'administration/home', 'refresh');
    }
}

/* End of file login.php */
/* Location: ./application/controllers/login.php */